import time
from utils import (
  log_message
)
from tmdb_api import (
  get_movie_keywords_by_id,
  get_movie_credits_by_id,
  get_movie_info_by_id,
)
from process_file_data import (
  get_data_from_csv,
  get_data_from_json,
  store_data_in_json,
)
from mongo import (
  collection_movie_info,
  collection_movie_credits,
  collection_movie_keywords,
  insert_data,
  get_collection_data_length
)

from settings import (
  COLLECTION_MOVIE_INFO,
  COLLECTION_MOVIE_CREDITS,
  COLLECTION_MOVIE_KEYWORDS,
)

# 存储links数据为json，以便后续处理
def store_links_data():
  result = get_data_from_csv('links.csv')
  store_data_in_json(result, 'links.json')

# 存储电影基本信息
def store_movie_data(collection_name):

  if collection_name == COLLECTION_MOVIE_INFO:
    collection = collection_movie_info
    get_api_function = get_movie_info_by_id
  elif collection_name == COLLECTION_MOVIE_CREDITS:
    collection = collection_movie_credits
    get_api_function = get_movie_credits_by_id
  elif collection_name == COLLECTION_MOVIE_KEYWORDS:
    collection = collection_movie_keywords
    get_api_function = get_movie_keywords_by_id
  
  # 获取tmdb_id列表
  json_file_name = 'links.json'
  id_list = get_data_from_json(json_file_name)

  # 这里主要是防止中断导致所有数据丢失，记录存储了多少条数据，然后从中间开始存储数据
  total_length = len(id_list)
  start_index = get_collection_data_length(collection)
  count = start_index
  for index in range(start_index, total_length):
    tmdb_id = id_list[index]['tmdbId']
    # 通过api获取数据
    try:
      data = get_api_function(tmdb_id)
    except Exception as e:
      log_message("出现异常,一分钟后再执行")
      log_message(f"异常内容：{e}")
      time.sleep(60)  # 等待10分钟
      # 再次执行这个函数
      store_movie_data(collection_name)
      return
      
    # 存储数据
    insert_data(collection, data)

    count += 1
    if count % 20 == 0:
      log_message("存储%s数据进度：%.2f%%" % (collection_name, count / total_length * 100))
  

# 主要函数
def store_tmdb_movie_data():
  # 将links.csv数据变成.json
  # store_links_data()
  
  store_movie_data(COLLECTION_MOVIE_INFO)
  log_message("存储%s数据完毕" % COLLECTION_MOVIE_INFO)
  
  store_movie_data(COLLECTION_MOVIE_CREDITS)
  log_message("存储%s数据完毕" % COLLECTION_MOVIE_CREDITS)
  
  store_movie_data(COLLECTION_MOVIE_KEYWORDS)
  log_message("存储%s数据完毕" % COLLECTION_MOVIE_KEYWORDS)


# store_tmdb_movie_data()

